package com.pms.privilege.user.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pms.privilege.user.model.UserModel;
import com.pms.privilege.user.service.UserServiceBase;
import com.pms.utils.Constant;
import com.pms.utils.CookieManage;
import com.pms.utils.MD5;

/**
 * 用户管理
 * 
 * @author huyinsong
 * 
 */
@Controller
public class UserController {
	@Resource(name = "userservice01")
	private UserServiceBase usb;

	/**
	 * 登陆用户管理主页
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/userindex.pms")
	public String userIndex(HttpServletRequest req, ModelMap mm) {
		CookieManage cm = new CookieManage();
		Map<String, Cookie> map = new HashMap<String, Cookie>();
		map = cm.getCookies(req);
		if (map!=null&&map.size()>0&&map.containsKey("permission")) {
			String permission = map.get("permission").getValue();
			if (permission != null && "yes".equals(permission)) {
				List<UserModel> userlist = usb.UserListService(0, 10);
				mm.addAttribute("userlist", userlist);
				return "user/userindex";
			}
		}
		return "redirect:login.pms";

	}

	/**
	 * 新增用户
	 * 
	 * @param req
	 */
	@RequestMapping(value = "/userinsert.pms", method = RequestMethod.POST)
	public void userInsert(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String user_name = req.getParameter("user_name");
			String user_password = req.getParameter("user_password");
			String user_nickname = req.getParameter("user_nickname");
			String user_email = req.getParameter("user_email");
			String user_phone_no = req.getParameter("user_phone_no");
			String valid = req.getParameter("user_valid");
			UserModel um = new UserModel();
			um.setUser_name(user_name);
			um.setUser_nickname(user_nickname);
			um.setUser_password(MD5.getMD5Code(user_password));
			um.setUser_phone_no(user_phone_no);
			um.setUser_email(user_email);
			if (valid.equals("yes")) {
				um.setUser_valid(Constant.valid.yes);
			} else {
				um.setUser_valid(Constant.valid.no);
			}
			JSONObject json = new JSONObject();

			int result = usb.insertUserService(um);
			if (result != -1) {
				json.put("result", result);
				json.put("msg", "OK");
			} else {
				json.put("result", result);
				json.put("msg", "OK");
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 修改用户
	 * 
	 * @param req
	 */
	@RequestMapping(value = "/userupdate.pms", method = RequestMethod.POST)
	public void userUpdate(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String user_id = req.getParameter("user_id");
			String user_nickname = req.getParameter("user_nickname");
			String user_email = req.getParameter("user_email");
			String user_phone_no = req.getParameter("user_phone_no");
			String valid = req.getParameter("user_valid");
			UserModel um = new UserModel();
			um.setUser_id(Long.parseLong(user_id));
			um.setUser_nickname(user_nickname);
			um.setUser_phone_no(user_phone_no);
			um.setUser_email(user_email);
			if (valid.equals("yes")) {
				um.setUser_valid(Constant.valid.yes);
			} else {
				um.setUser_valid(Constant.valid.no);
			}
			JSONObject json = new JSONObject();

			int result = usb.updateUserService(um);
			if (result != -1) {
				json.put("result", result);
				json.put("msg", "OK");
			} else {
				json.put("result", result);
				json.put("msg", "OK");
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除
	 * 
	 * @param req
	 */
	@RequestMapping(value = "/userdelete.pms", method = RequestMethod.POST)
	public void userDelete(String user_id, HttpServletResponse resp) {
		try {
			JSONObject json = new JSONObject();
			int result = usb.deleteUserService(Long.parseLong(user_id));
			if (result != -1) {
				json.put("result", result);
				json.put("msg", "OK");
			} else {
				json.put("result", result);
				json.put("msg", "OK");
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 检索所有用户列表
	 * @param req
	 * @param resp 返回json列表
	 */
	@RequestMapping(value="userinfo.pms",method=RequestMethod.POST)
	public void userInfoList(HttpServletRequest req,HttpServletResponse resp){
		
		try {
			List<UserModel> userlist = usb.UserListService(0, 10);
			JSONArray userArray=new JSONArray();
			for(UserModel um:userlist){
				JSONObject json=new JSONObject(um);
				userArray.put(json);
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(userArray);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询所有没有权限的用户
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value="userinfowithoutperm.pms",method=RequestMethod.POST)
	public void userWithoutPermList(HttpServletRequest req,HttpServletResponse resp){
		try {
			String func_id=req.getParameter("func_id");
			List<UserModel> userlist = new ArrayList<UserModel>();
			JSONArray userArray=new JSONArray();
			if(func_id!=null&&!"".equals(func_id)){
				userlist=usb.userWithoutPermService(Long.parseLong(func_id),0,10);
				if(userlist.size()>0){
					for(UserModel um:userlist){
						JSONObject json=new JSONObject(um);
						userArray.put(json);
					}
				}
			
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(userArray);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
